package com.pla;

import java.io.FileInputStream;
import java.io.FileNotFoundException;

import com.pla.persistence.Vine;
import com.pla.persistence.VineDao;

import android.app.Activity;
import android.content.Intent;
import android.database.DataSetObserver;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.RemoteViews.ActionException;

public class Vinown extends Activity {

	private final static int REQUEST_CODE_PHOTO = 1;
	private final VineDao vineDao = new VineDao();
	private ListAdapter listAdapter;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		// setup photo button
		Button photoBtn = (Button) findViewById(R.id.Button01);
		photoBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				Log.d("vinown", "onClick");

				Intent captureIntent = new Intent(
						MediaStore.ACTION_IMAGE_CAPTURE);
				// TODO: Momentan wird nur ein kleines image gespeichert. Um ein
				// groesseres speichern zu koennen, muss das extraflat
				// "MediaStore.EXTRA_OUTPUT" gesetzt werden. Dabei muss
				// eine URI zu einem Content-Provider uebergeben werden.
				startActivityForResult(captureIntent, REQUEST_CODE_PHOTO);

				// Das Starten der Photo-Activity wurde durch die Verwendung der
				// von Android bereitgestellten Activity obsolet.
				// startActivityForResult(new Intent(getApplicationContext(),
				// PhotoActivity.class), REQUEST_CODE_PHOTO);
			}
		});
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (requestCode == REQUEST_CODE_PHOTO && RESULT_OK == resultCode) {
			Log.d("com.pla", "picture-result: " + data);

			Bitmap bm = (Bitmap) data.getExtras().get("data");

			// TODO: save in SQL-Lite DB (maybe via own content-provider)
		}
	}

	@Override
	protected void onResume() {
		super.onResume();

		// try {
		// FileInputStream openFileInput = getApplicationContext()
		// .openFileInput("myFile0.jpg");
		//
		// ImageView image = (ImageView) findViewById(R.id.ImageView01);
		// image.setImageBitmap(BitmapFactory.decodeStream(openFileInput));
		// } catch (FileNotFoundException e) {
		// Log.w("com.pla", "No Image-file found!");
		// }

		// show vine list
		listAdapter = new ArrayAdapter<Vine>(this,
				android.R.layout.simple_list_item_1, vineDao.getOrderedVines());
		ListView listView = (ListView) findViewById(R.id.ListView01);
		listView.setAdapter(listAdapter);
		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// Vine selectedVine = (Vine) arg0.getSelectedItem();

				// Log.d("com.pla", "value via adapter: " +
				// listAdapter.getItem(arg2));
				// Log.d("com.pla", "selectedItem: " + arg0.getSelectedItem());
				// Log.d("com.pla", "selectedItemId: " +
				// arg0.getSelectedItemId());
				// Log.d("com.pla", "selectedView: " + arg0.getSelectedView());
				// Log.d("com.pla", "selectedItemPosition: "
				// + arg0.getSelectedItemPosition());

				Vine selectedVine = (Vine) listAdapter.getItem(arg2);

				// start detail activity
				Intent detailIntent = new Intent(getApplicationContext(),
						VineDetailActivity.class);
				detailIntent.putExtra(VineDetailActivity.DATA_KEY_VINE_ID,
						selectedVine.getId());
				startActivity(detailIntent);
			}
		});
	}
}